﻿/******************************************************************************

    Copyright (c) 2010, Sergey Mudrov
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.
        * Neither the name of Sergey Mudrov nor the
          names of its contributors may be used to endorse or promote products
          derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SERGEY MUDROV BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Gets the albums by user ID.
        /// </summary>
        /// <param name="uid">The user ID.</param>
        /// <param name="aids">The album IDs.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Albums.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_GetAlbums(long uid, long[] aids, ApiCallback<Album[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));

            if (aids != null && aids.Length > 0)
            {
                string aidsString = String.Empty;

                foreach (int i in aids)
                    aidsString += i.ToString() + ",";

                aidsString = aidsString.Remove(aidsString.Length - 1);

                list.Add(new Parameter("aids", aidsString));
            }

            return this.SendRequest("photos.getAlbums", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the photos by user ID and album ID.
        /// </summary>
        /// <param name="uid">The user ID.</param>
        /// <param name="aid">The album ID.</param>
        /// <param name="pids">The photo IDs.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Photos.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_Get(long uid, long aid, long[] pids, ApiCallback<Photo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("aid", aid.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));

            if (pids != null && pids.Length > 0)
            {
                string pidString = String.Empty;

                foreach (int i in pids)
                    pidString += i.ToString() + ",";

                pidString = pidString.Remove(pidString.Length - 1);

                list.Add(new Parameter("pids", pidString));
            }

            return this.SendRequest("photos.get", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the photo by its owners IDs. In format: userid_photoid,... 
        /// </summary>
        /// <param name="ids">The user's IDs.</param>
        /// <param name="photos">The photo's IDs.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Photos.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_GetById(long[] ids, long[] photos, ApiCallback<Photo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();

            if (photos != null && photos.Length > 0 && photos.Length==ids.Length)
            {
                string photosString = "";
                for (int i = 0; i < photos.Length;i++)
                    photosString += ids[i].ToString() +"_"+photos[i]+ ",";
                photosString = photosString.Remove(photosString.Length - 1);
                list.Add(new Parameter("photos", photosString));
            }

            return this.SendRequest("photos.getById", callback, list.ToArray());
        }

        /// <summary>
        /// Creates the album.
        /// </summary>
        /// <param name="title">The title of the album.</param>
        /// <param name="privacy">The privacy of the album.</param>
        /// <param name="comment_privacy">The privacy comment.</param>
        /// <param name="description">The description.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>New Album object.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_CreateAlbum(string title, int privacy, int comment_privacy, string description, ApiCallback<Album> callback)
        {
            List<Parameter> list = new List<Parameter>();

            list.Add(new Parameter("title", title));
            list.Add(new Parameter("privacy", privacy.ToString()));
            list.Add(new Parameter("comment_privacy", comment_privacy.ToString()));
            list.Add(new Parameter("description", description));

            return this.SendRequest("photos.createAlbum", callback, list.ToArray());
        }

        /// <summary>
        /// Edits the album.
        /// </summary>
        /// <param name="aid">The album ID.</param>
        /// <param name="title">The title of the album.</param>
        /// <param name="privacy">The privacy of the album.</param>
        /// <param name="comment_privacy">The privacy comment.</param>
        /// <param name="description">The description.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_EditAlbum(long aid, string title, int privacy, int comment_privacy, string description, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();

            list.Add(new Parameter("aid", aid.ToString()));
            list.Add(new Parameter("title", title));
            list.Add(new Parameter("privacy", privacy.ToString()));
            list.Add(new Parameter("comment_privacy", comment_privacy.ToString()));
            list.Add(new Parameter("description", description));

            return this.SendRequest("photos.editAlbum", callback, list.ToArray());
        }

        /// <summary>
        /// Get URL of upload server for photos.
        /// </summary>
        /// <param name="aid">The album ID, where you want to upload the photos.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Upload server information.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_GetUploadServer(long aid, ApiCallback<PhotoUploadServer> callback)
        {
            return this.SendRequest("photos.getUploadServer", callback, new Parameter[] { new Parameter("aid", aid.ToString()) });
        }

        /// <summary>
        /// Get URL of upload server for photos.
        /// </summary>
        /// <param name="aid">The album ID, where you want to upload the photos.</param>
        /// <param name="save_big">1 - to save in big resolutions also, 0 - by default.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Upload server information.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_GetUploadServer(long aid, int save_big, ApiCallback<PhotoUploadServer> callback)
        {
            return this.SendRequest("photos.getUploadServer", callback, new Parameter[] { new Parameter("aid", aid.ToString()), new Parameter("save_big", save_big.ToString()) });
        }

        /// <summary>
        /// Saves the photo after successful uploading.
        /// </summary>
        /// <param name="aid">The album for uploading.</param>
        /// <param name="server">The server.</param>
        /// <param name="photoslist">The photo.</param>
        /// <param name="hash">The hash.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of photos.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_Save(long aid, string server, string photoslist, string hash, ApiCallback<Photo[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("aid", aid.ToString()));
            list.Add(new Parameter("server", server));
            list.Add(new Parameter("photos_list", photoslist));
            list.Add(new Parameter("hash", hash));
            return this.SendRequest("photos.save", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the URL address of server for photos uploading on the users page.
        /// </summary>        
        /// <param name="callback">The callback.</param>
        /// <returns>Profile server information.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_GetProfilesUploadServer(ApiCallback<ProfileUploadServer> callback)
        {
            return this.SendRequest("photos.getProfileUploadServer", callback, null);
        }

        /// <summary>
        /// Saves the photo after succesful uploading.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="photoslist">The photos.</param>
        /// <param name="hash">The hash.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Photo information.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_SaveProfilePhotos(string server, string photoslist, string hash, ApiCallback<ProfilePhoto> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("server", server));
            list.Add(new Parameter("photos_list", photoslist));
            list.Add(new Parameter("hash", hash));
            return this.SendRequest("photos.saveProfilePhoto", callback, list.ToArray());
        }

        /// <summary>
        /// Moving photo from one album to another.
        /// </summary>
        /// <param name="pid">The photo to move.</param>
        /// <param name="target_id">The album, where photo is moving to.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_Move(long pid, long target_id, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("pid", pid.ToString()));
            list.Add(new Parameter("target_id", target_id.ToString()));
            return this.SendRequest("photos.move", callback, list.ToArray());
        }

        /// <summary>
        /// Moving photo from one album to another.
        /// </summary>
        /// <param name="pid">The photo to move.</param>
        /// <param name="target_id">The album, where photo is moving to.</param>
        /// <param name="oid">The ID of photo owner, current user ID by default.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_Move(long pid, long target_id, long oid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("pid", pid.ToString()));
            list.Add(new Parameter("target_id", target_id.ToString()));
            list.Add(new Parameter("oid", oid.ToString()));
            return this.SendRequest("photos.move", callback, list.ToArray());
        }

        /// <summary>
        /// Makes the photo a cover of album.
        /// </summary>
        /// <param name="pid">The photo for album.</param>
        /// <param name="aid">The album ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_MakeCover(long pid, long aid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("pid", pid.ToString()));
            list.Add(new Parameter("aid", aid.ToString()));
            return this.SendRequest("photos.makeCover", callback, list.ToArray());
        }

        /// <summary>
        /// Makes the photo a cover of album.
        /// </summary>
        /// <param name="pid">The photo for album.</param>
        /// <param name="target_id">The album ID.</param>
        /// <param name="oid">The ID of album owner, current user ID by default.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_MakeCover(long pid, long target_id, long oid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("pid", pid.ToString()));
            list.Add(new Parameter("aid", target_id.ToString()));
            list.Add(new Parameter("oid", oid.ToString()));
            return this.SendRequest("photos.makeCover", callback, list.ToArray());
        }

        /// <summary>
        /// Changes the album position.
        /// </summary>
        /// <param name="aid">The album ID.</param>
        /// <param name="before">The ID of album before which should be placed.</param>
        /// <param name="after">The ID of album after which should be placed.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_ReorderAlbums(long aid, long before, long after, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("aid", aid.ToString()));
            list.Add(new Parameter("before", before.ToString()));
            list.Add(new Parameter("after", after.ToString()));
            return this.SendRequest("photos.reorderAlbum", callback, list.ToArray());
        }

        /// <summary>
        /// Changes the photo position.
        /// </summary>
        /// <param name="aid">The photo ID.</param>
        /// <param name="before">The ID of photo before which should be placed.</param>
        /// <param name="after">The ID of photo after which should be placed.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        /// <added_by>Anton Gubarenko</added_by>
        public long Photos_ReorderPhotos(long aid, long before, long after, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("aid", aid.ToString()));
            list.Add(new Parameter("before", before.ToString()));
            list.Add(new Parameter("after", after.ToString()));
            return this.SendRequest("photos.reorderPhotos", callback, list.ToArray());
        }
    }
}
